<?php
include("include/config.inc.php");
include("./imageFunction.php");
if(!isset($_SESSION['s_activIds']))
{
	header("Location:checkLogin.php");
}
else
{
	if(isset($_REQUEST['submit']))
	{
		$userId       = isset($_POST['userId']) ? $_POST['userId'] : 0;
	  $activeUserId = ($_SESSION['s_activIds']);
	
	  $uploadDateTime = date('Y-m-d h:i:s');
	  
	  $insertItem  = "INSERT INTO userdoc (userId,uploadFile,uploadedBy,uploadDateTime)
	                                VALUES(".$activeUserId.",'".$_FILES['uploadFile']['name']."',".$activeUserId.",'".$uploadDateTime."')";
	  $insertItemRes = mysql_query($insertItem);
	  $lastItemId = mysql_insert_id();
	  
	  if(isset($_FILES["uploadFile"]["name"]))
	 	{
	  	$mainFileName  =  $_FILES["uploadFile"]["name"];
			$image = $lastItemId."_".$mainFileName;
			$file = "uploadDoc/".$image; 
			if (move_uploaded_file($_FILES['uploadFile']['tmp_name'], $file)) 
			{ 
			  $imageParameter = array(); 
				$thumbnail = new thumbnail();
				//get image size
				list($width, $height) = getimagesize($file);
				// Create Thubnail::::
				if(($width > $cfgThumbnailWidth) || ($height > $cfgThumbnailHeight))
				{
				  $imageParameter = setImageHightWigth($height,$width,$cfgThumbnailWidth,$cfgThumbnailHeight);
				  $thumbnail->generate("uploadDoc/".$image,$image,"uploadDoc/thumb/",$image,$imageParameter['newWidth'], $imageParameter['newHeight']);
				}
				else
				  @copy("uploadDoc/".$image,"uploadDoc/thumb/".$image);
				// Create Thubnail End::::
			}
			else 
			{
				echo "error";
			}
		}
		$updateImage = "UPDATE userdoc
	                     SET uploadFile = '".$image."'
	                   WHERE userDocId = ".$lastItemId;
	  $updateImageRes = mysql_query($updateImage);
	  if(!$insertItemRes)
	  {
	  	die ("Insert Query Not Insertd :" .mysql_error());    
	  }
	  else
	  {
	    header("Location:uploadDoc.php");
	  }
	}
	include("bottom.php");
	$smarty->display("uploadDoc.tpl");
}
?>